<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/17 12:40
 */

namespace app\common\lib;


class Arr
{
    /**
     * 功能说明 权限列表转换成树形结构
     * @author: wang
     * @Date: 2020/3/24 18:30
     * @param $cate
     * @param string $lefthtml
     * @param int $pid
     * @param int $lvl
     * @param int $leftpin
     * @return array
     */
    public function rule($cate, $lefthtml = '— — ', $pid = 0, $lvl = 0, $leftpin = 0)
    {
        $arr = array();
        foreach ($cate as $v) {
            if ($v['pid'] == $pid) {
                $v['lvl'] = $lvl + 1;
                $v['leftpin'] = $leftpin + 0;//左边距
                $v['lefthtml'] = str_repeat($lefthtml, $lvl);
                $arr[] = $v;
                $arr = array_merge($arr, self::rule($cate, $lefthtml, $v['id'], $lvl + 1, $leftpin + 20));
            }
        }
        return $arr;
    }


    /**
     * 功能说明 权限数组转换树形结构数组
     * @author: wang
     * @Date: 2020/4/1 18:44
     * @param $data
     * @param $rules
     * @return array
     */
    public function arrToAuthArr($data, $rules)
    {
        $arr = [];
        foreach ($data as $k => $v) {
            $arr[$k]['title'] = $v['title'];
            $arr[$k]['id'] = $v['id'];
            $arr[$k]['checked'] = false;
            $arr[$k]['spread'] = true;
            $arr[$k]['level'] = $v['lvl'];
            $arr[$k]['pid'] = $v['pid'];
            $arr[$k]['title'] = $v['title'];
            $arr[$k]['icon'] = $v['icon'];
            $arr[$k]['sort'] = $v['sort'];
            $arr[$k]['name'] = $v['name'];
            foreach ($rules as $kk => $vv) {
                if ($v['id'] == $vv) {
                    $arr[$k]['checked'] = true;
                }
            }
        }

        $three = [];
        $new_arr = [];
        foreach ($arr as $k => $v) {
            if ($v['level'] == 1) {
                $new_arr[$k]['title'] = $v['title'];
                $new_arr[$k]['icon'] = $v['icon'];
                $new_arr[$k]['sort'] = $v['sort'];
                $new_arr[$k]['name'] = $v['name'];
                $new_arr[$k]['id'] = $v['id'];
                $new_arr[$k]['checked'] = $v['checked'];
                $new_arr[$k]['spread'] = $v['spread'];
                $new_arr[$k]['level'] = $v['level'];
                $new_arr[$k]['pid'] = $v['pid'];
            }
            if ($v['level'] == 3) {
                $three[$k]['title'] = $v['title'];
                $three[$k]['icon'] = $v['icon'];
                $three[$k]['sort'] = $v['sort'];
                $three[$k]['name'] = $v['name'];
                $three[$k]['id'] = $v['id'];
                $three[$k]['checked'] = $v['checked'];
                $three[$k]['spread'] = $v['spread'];
                $three[$k]['level'] = $v['level'];
                $three[$k]['pid'] = $v['pid'];
            }
            $num = 0;
            foreach ($arr as $kk => $vv) {
                if ($vv['level'] == 2 && $v['id'] == $vv['pid']) {
                    $new_arr[$k]['children'][$num]['title'] = $vv['title'];
                    $new_arr[$k]['children'][$num]['icon'] = $vv['icon'];
                    $new_arr[$k]['children'][$num]['sort'] = $vv['sort'];
                    $new_arr[$k]['children'][$num]['name'] = $vv['name'];
                    $new_arr[$k]['children'][$num]['id'] = $vv['id'];
                    $new_arr[$k]['children'][$num]['checked'] = $vv['checked'];
                    $new_arr[$k]['children'][$num]['spread'] = $vv['spread'];
                    $new_arr[$k]['children'][$num]['level'] = $vv['level'];
                    $new_arr[$k]['children'][$num]['pid'] = $vv['pid'];
                    $num++;
                }
            }
        }

        foreach ($new_arr as $k => $v) {
            if (!empty($v['children'])) {
                foreach ($v['children'] as $k2 => $v2) {
                    $num = 0;
                    foreach ($three as $k3 => $v3) {
                        if ($v2['id'] == $v3['pid']) {
                            $new_arr[$k]['children'][$k2]['checked'] = false;
                            $new_arr[$k]['children'][$k2]['children'][$num]['title'] = $v3['title'];
                            $new_arr[$k]['children'][$k2]['children'][$num]['icon'] = $v3['icon'];
                            $new_arr[$k]['children'][$k2]['children'][$num]['sort'] = $v3['sort'];
                            $new_arr[$k]['children'][$k2]['children'][$num]['name'] = $v3['name'];
                            $new_arr[$k]['children'][$k2]['children'][$num]['id'] = $v3['id'];
                            $new_arr[$k]['children'][$k2]['children'][$num]['checked'] = $v3['checked'];
                            $new_arr[$k]['children'][$k2]['children'][$num]['spread'] = $v3['spread'];
                            $new_arr[$k]['children'][$k2]['children'][$num]['level'] = $v3['level'];
                            $new_arr[$k]['children'][$k2]['children'][$num]['pid'] = $v3['pid'];
                            $num++;
                        }
                    }
                }
                $new_arr[$k]['checked'] = false;
            }

        }

        $new_arr = array_values($new_arr);
        return $new_arr;
    }



}